<?php
namespace modules\article;

use modules\article\model\Article as ArticleModel;

class Article {
  public function __construct(ArticleModel $article) {
    $this->article = $article;
  }

  public function data() {
    return $this->article->get();
  }

  public function updateArt($res) {
    // return $res;
    return $this->article->where("id", $res['id'])->update([
      "cid" => $res['cid'],
      "title" => $res['title'],
      "author" => $res['author'],
      "image" => $res['image'],
      "show" => $res['show'],
      "views" => $res['views'],
      "synopsis" => $res['synopsis'],
      "isPay" => $res['isPay'],
      "price" => $res['price']
    ]);
  }

  public function addArt($res) {
    // return $res;
    return $this->article->insert($res);
  }

  public function delArt($res) {
    return $this->article->where('id' ,$res['id'])->delete();
  }

  public function getArtItem($res) {
    return ($this->article->where('id', $res['id'])->get())[0];
  }

  // 这里要使用 like 进行模糊匹配 
  // eg: select * from article title like '%'+$res['title']+'%'
  // 暂时就单纯在它前面多加一个 where, 但这样并不工整
  public function search($res) {
    // echo "%".($res['title'])."%";
    return $this->article->like('title', '%'.$res['title'].'%')->get();
  }
}

?>